package com.cheetah.start.system.log.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cheetah.start.common.base.result.ResultController;
import com.cheetah.start.common.base.result.ResultTable;
import com.cheetah.start.common.enums.LoggingType;
import com.cheetah.start.common.query.PageQuery;
import com.cheetah.start.system.log.domain.SysLog;
import com.cheetah.start.system.log.service.SysLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author x
 * @create 2021/7/17
 **/
@Slf4j
@Validated
@RestController
@RequestMapping("/log")
public class SysLogController extends ResultController {
    @Autowired
    private SysLogService sysLogService;


    /**
     * 登录日志列表
     *
     * @param pageQuery 页面查询
     * @return {@link ResultTable}
     */
    @GetMapping("/loginList")
    public ResultTable list(@Validated PageQuery pageQuery) {
        Page<SysLog> page = new Page(pageQuery.getPage(), pageQuery.getLimit());
        QueryWrapper<SysLog> wrapper = new QueryWrapper<>();
        wrapper.eq("logging_type", LoggingType.LOGIN)
                .orderByDesc("create_time");
        sysLogService.page(page, wrapper);
        return pageTable(page.getRecords(), page.getTotal());
    }

    /**
     * 操作日志列表
     *
     * @param pageQuery 页面查询
     * @return {@link ResultTable}
     */
    @GetMapping("/operateList")
    public ResultTable operatelist(@Validated PageQuery pageQuery) {
        Page<SysLog> page = new Page(pageQuery.getPage(), pageQuery.getLimit());
        QueryWrapper<SysLog> wrapper = new QueryWrapper<>();
        wrapper.eq("logging_type", LoggingType.OPERATE)
                .orderByDesc("create_time");
        sysLogService.page(page, wrapper);
        return pageTable(page.getRecords(), page.getTotal());
    }
}
